package com.example.com.test.activity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.com.test.R;
import com.example.com.test.player.MyMediaPlayer;
import com.example.com.test.util.DateUtils;
import com.warkiz.widget.IndicatorSeekBar;
import com.warkiz.widget.OnSeekChangeListener;
import com.warkiz.widget.SeekParams;

/**
 * 作者： njb
 * 时间： 2018/11/16 22:56
 * 描述：
 * 来源：
 */
public class MediaPlayerActivity extends AppCompatActivity {
    private IndicatorSeekBar seekBar;
    private Button button ;
    private TextView startTimeTv,endTimeTv;
//    private static final String url = "https://zhlmoss.oss-cn-beijing.aliyuncs.com/uploadFileFolder/music/20181109172641_664.mp3";
    private String url = "http://image.38.hn/public/attachment/201808/20/15/5b7a727b88311.mp3";
    private ProgressBar progressBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);
        initView();
        initListener();
    }



    private void initView() {
        button = findViewById(R.id.btn_player);
        seekBar = findViewById(R.id.audio_seekBar);
        startTimeTv = findViewById(R.id.tv_start_time);
        endTimeTv = findViewById(R.id.tv_end_time);
        progressBar = findViewById(R.id.progressbar);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(MyMediaPlayer.getInstance().isPlaying()){
                    MyMediaPlayer.getInstance().pause();
                }else {
                    MyMediaPlayer.getInstance().start(url);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        //同步播放进度
        MyMediaPlayer.getInstance().setMyController(new MyMediaPlayer.MyController() {
            @Override
            public void onProgress(int progress) {
                if (seekBar != null) {
                    seekBar.setProgress(progress);
                }
                if(progressBar != null){
                    progressBar.setProgress(progress);
                }

                if (startTimeTv != null) {
                    startTimeTv.setText(DateUtils.getTimeByLong(MyMediaPlayer.getInstance().getCurrentPosition()));
                }

                if(endTimeTv != null){
                    endTimeTv.setText(DateUtils.getTimeByLong(MyMediaPlayer.getInstance().getCurrentPosition()));
                }

            }
        });
    }


    private void initListener() {
        seekBar.setOnSeekChangeListener(new OnSeekChangeListener() {
            @Override
            public void onSeeking(SeekParams seekParams) {

            }

            @Override
            public void onStartTrackingTouch(IndicatorSeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(IndicatorSeekBar seekBar) {
                MyMediaPlayer.getInstance().seekToPosition(seekBar.getProgress());
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyMediaPlayer.getInstance().stop();
    }
}
